package com.zch.book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zch.book.entity.Book;
import com.zch.book.service.BookService;
import com.zch.book.service.impl.BookServiceImpl;
import com.zch.book.util.DateFormat;

/**
 * Servlet implementation class BookServlet
 */
// @WebServlet("/BookServlet")
public class BookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	BookService bs = new BookServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		if ("getAllBook".equals(method)) {
			getAllBook(request, response);
		} else if ("getBookByNo".equals(method)) {
			getBookByNo(request, response);
		} else if ("addBook".equals(method)) {
			addBook(request, response);
		} else if ("deleteBook".equals(method)) {
			deleteBook(request, response);
		} else if ("updateBook".equals(method)) {
			updateBook(request, response);
		}
	}

	protected void getAllBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 接收当前页码pageIndex
		String pageIndexStr = request.getParameter("pageIndex");
		if (pageIndexStr == null || pageIndexStr.length() == 0) {
			pageIndexStr = "1";
		}
		// 转换页码数据类型,页码不可能为空
		int pageIndex = Integer.parseInt(pageIndexStr);
		// 获取图书名称
		String bookName = request.getParameter("bookName");
		// 获取图书作者
		String bookAuthor = request.getParameter("bookAuthor");
		// 获取图书的出版日期
		String startBookDateStr = request.getParameter("startBookDate");
		String endBookDateStr = request.getParameter("endBookDate");
		// 转换日期数据类型
		Date startBookDate = DateFormat.to_Date2(startBookDateStr);
		Date endBookDate = DateFormat.to_Date2(endBookDateStr);
		// 调用dervice层的模糊查询的方法得到图书列表
		List<Book> list = bs.getBookByPage(bookName, bookAuthor, startBookDate, endBookDate, pageIndex);
		// 调用service层方法获取查询到的数据的总页码
		int totalPage = bs.getTotalCount(bookName, bookAuthor, startBookDate, endBookDate);
		// 使用request域存放list集合和各种参数并转发
		request.setAttribute("list", list);
		request.setAttribute("bookName", bookName);
		request.setAttribute("bookAuthor", bookAuthor);
		request.setAttribute("startBookDate", startBookDateStr);
		request.setAttribute("endBookDate", endBookDateStr);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("i",(pageIndex-1)*4);
		request.getRequestDispatcher("book.jsp").forward(request, response);
	}

	protected void getBookByNo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String bookNoStr = request.getParameter("bookNo");
		int bookNo = Integer.parseInt(bookNoStr);
		// 调用service层的方法获得图书对象
		Book book = bs.getBookByBookNo(bookNo);
		// 将图书对象存入request作用域
		request.setAttribute("book", book);
		// 转发到修改页面
		request.getRequestDispatcher("update.jsp").forward(request, response);
	}

	protected void addBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String bookNoStr = request.getParameter("bookNo");
		int bookNo = Integer.parseInt(bookNoStr);
		String bookName = request.getParameter("bookName");
		String bookAuthor = request.getParameter("bookAuthor");
		String bookDateStr = request.getParameter("bookDate");
		Date bookDate = DateFormat.to_Date2(bookDateStr);
		String bookDescription = request.getParameter("bookDescription");
		Book book = new Book(bookNo, bookName, bookAuthor, bookDate, bookDescription);
		// 调用service层的添加方法添加图书信息
		boolean flag = bs.addBook(book);
		// 实例化一个out对象
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将添加成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加成功！');window.location.href='BookServlet?method=getAllBook'</script>");
		} else {
			// 添加失败,将添加失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加失败！');window.location.href='BookServlet?method=getAllBook'</script>");
		}
	}

	protected void deleteBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String bookNoStr = request.getParameter("bookNo");
		int bookNo = Integer.parseInt(bookNoStr);
		// 调用service层的方法删除数据库中图书
		boolean flag = bs.deleteBook(bookNo);
		// 实例化一个out对象
		PrintWriter out = response.getWriter();
		if (flag) {
			// 删除成功,将删除成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除成功！');window.location.href='BookServlet?method=getAllBook'</script>");
		} else {
			// 删除失败,将删除失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除失败！');window.location.href='BookServlet?method=getAllBook'</script>");
		}
	}

	protected void updateBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String bookNoStr = request.getParameter("bookNo");
		int bookNo = Integer.parseInt(bookNoStr);
		String bookName = request.getParameter("bookName");
		String bookAuthor = request.getParameter("bookAuthor");
		String bookDateStr = request.getParameter("bookDate");
		Date bookDate = DateFormat.to_Date2(bookDateStr);
		String bookDescription = request.getParameter("bookDescription");
		// 实例化图书对象
		Book book = new Book(bookNo, bookName, bookAuthor, bookDate, bookDescription);
		// 调用service层里面的更新方法
		boolean flag = bs.updateBook(book);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 更新成功,将更新成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('更新成功！');window.location.href='BookServlet?method=getAllBook'</script>");
		} else {
			// 更新失败,将更新失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('更新失败！');window.location.href='BookServlet?method=getAllBook'</script>");
		}
	}

}
